package com.sxpi.course.service.course;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.sxpi.course.dto.CourseBuyDto;
import com.sxpi.course.dto.CourseDto;
import com.sxpi.course.mapper.CourseMapper;
import com.sxpi.course.pojo.Course;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.h2.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/13 13:04
 */
@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper,Course> implements ICourseService {

    /**
     * 查询课程产品  -用于缓存预热
     * @return
     */
    @Override
    public List<Course> findCacheCourse() {
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(Course::getStatus,1);
        courseLambdaQueryWrapper.eq(Course::getIsdelete,0);
        return this.list(courseLambdaQueryWrapper);
    }

    /**
     * 查询课程信息并分页和搜索
     * @return
     */
    @Override
    public Page<Course> findCoursePage(CourseDto courseDto) {
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Course> coursePage = new Page<>(courseDto.getPageNo(), courseDto.getPageSize());
        // 这里面会存在大字段
        courseLambdaQueryWrapper.select(
                Course::getId,
                Course::getImg,
                Course::getTitle,
                Course::getTags,
                Course::getCategoryid,
                Course::getCategorytitle,
                Course::getRealprice,
                Course::getDescription,
                Course::getViews,
                Course::getIshot,
                Course::getIsnew,
                Course::getIspush,
                Course::getCommission,
                Course::getCreateTime,
                Course::getUpdateTime
        );
        courseLambdaQueryWrapper.eq(Course::getStatus,1);
        courseLambdaQueryWrapper.eq(Course::getIsdelete,0);
        courseLambdaQueryWrapper.eq(courseDto.getCid() != null,Course::getCategoryid,courseDto.getCid());
        courseLambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(courseDto.getKeyword()),Course::getTitle,courseDto.getKeyword());
        courseLambdaQueryWrapper.orderByDesc(Course::getUpdateTime);
        return this.page(coursePage,courseLambdaQueryWrapper);
    }

    /**
     * 查询相关课程信息
     * @param courseDto
     * @return
     */
    @Override
    public List<Course> findCoursePageRelation(CourseDto courseDto) {
        // 分页
        Page<Course> coursePage = new Page<>(courseDto.getPageNo(), courseDto.getPageSize());
        // 设置条件
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 记得查询的不允许使用  * 因为表中存在大字段了。对jvm内存消耗是非常大的。
        lambdaQueryWrapper.select(
                Course::getId,
                Course::getImg,
                Course::getTitle,
                Course::getTags,
                Course::getCategoryid,
                Course::getCategorytitle,
                Course::getRealprice,
                Course::getViews,
                Course::getIshot,
                Course::getIsnew,
                Course::getDescription,
                Course::getIspush,
                Course::getCommission,
                Course::getCreateTime,
                Course::getUpdateTime
        );

        // 必要条件 发布的并且是未删除
        lambdaQueryWrapper.eq(Course::getStatus, 1);
        lambdaQueryWrapper.eq(Course::getIsdelete, 0);
        // 把自身排除掉
        lambdaQueryWrapper.ne(Course::getId,courseDto.getOpid());
        // 搜索条件
        lambdaQueryWrapper.eq(courseDto.getCid() != null, Course::getCategoryid, courseDto.getCid());
        // 排序
        lambdaQueryWrapper.orderByDesc(Course::getUpdateTime);
        // 返回
        Page<Course> page = this.page(coursePage, lambdaQueryWrapper);
        List<Course> list = page.getRecords();
        return CollectionUtils.isEmpty(list) ? new ArrayList<>() : list;
    }

    /**
     * 购买商品扣减库存
     * @param courseBuyDto
     * @return
     */
    @Override
    // @LcnTransaction
    @GlobalTransactional
    @Transactional(rollbackFor = Exception.class)
    public boolean miniusCourseStore(CourseBuyDto courseBuyDto) {
        Course course = this.getById(courseBuyDto.getId());
        Integer coursestore = course.getCoursestore();
        // 我这个是错误逻辑！！！
        // if(coursestore != null || coursestore <= 0 || coursestore <= courseBuyDto.getNum())
        // 库存不足或者0的情况或者小宇购买的数量
        if (coursestore == null || (coursestore != null && coursestore <= 0)
                || coursestore < courseBuyDto.getNum()) {
            // throw new RuntimeException("库存不足!!!!");
            return false;
        }
        Course newCourse = new Course();
        newCourse.setId(course.getId());
        // 商品总数量-1
        newCourse.setCoursestore(coursestore - courseBuyDto.getNum());
        boolean update = this.updateById(newCourse);
        return update;
    }

}
